
import CryptoJS from 'crypto-js';
function encrypt(key,iv,needJm,ciphertext) {
    const keyHex = CryptoJS.enc.Utf8.parse(key);
    const ivHex = CryptoJS.enc.Utf8.parse(iv);
    // CFB 模式，NoPadding
    const encrypted = CryptoJS.AES.encrypt(needJm, keyHex, {
        iv: ivHex,
        mode: CryptoJS.mode.CFB,
        padding: CryptoJS.pad.NoPadding
    });
    ciphertext = encrypted.toString();
    return ciphertext

};
function resetForm(form){
    for(let i in form){
        form[i] = ''
    }
}
function isPhone(value) {
  const reg = /^((13[0-9])|(14[5-7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/
  return reg.test(value)
}
function getValue(arr,val){    
    arr.forEach(item => {        
        if(item.label == val){
            val = item.value            
        }
        return
    })
    return val
}
function getLabel(arr,val){
    arr.forEach(item => {
        if(val == item.value){
            val = item.label
        }
    })
    return val
}
function recursionFun(arr,result = []){
    arr.forEach(item => {
        result.push({
            label : item.name,
            value : item.id
        })
        if(item.children?.length > 0){
            recursionFun(item.children,result)
        }
    })
    return result
}
function handleTree(data, id, parentId, children) {
  let config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children'
  }

  var childrenListMap = {}
  var tree = []
  for (let d of data) {
    let id = d[config.id]
    childrenListMap[id] = d
    if (!d[config.childrenList]) {
      d[config.childrenList] = []
    }
  }

  for (let d of data) {
    let parentId = d[config.parentId]
    let parentObj = childrenListMap[parentId]
    if (!parentObj) {
      tree.push(d)
    } else {
      parentObj[config.childrenList].push(d)
    }
  }
  return tree
}
function isInteger(obj) {
 return Math.floor(obj) === obj
}
export {
    encrypt,
    isPhone,
    resetForm,
    getValue,
    recursionFun,
    handleTree,
    getLabel,
    isInteger
}